Public Class CharaInfoViewer
    Private Month() As String = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}

    Public Function CharaInfoToHtml(ByRef ChData As CharactersInfo.Characters, Optional ByVal OnlyBody As Boolean = True) As String
        Dim html As String = ""
        Dim i As Integer

        If ChData.ClassName = "" Then Return ""

        Dim Armory As New AccessArmory
        Dim ArmoryUrlChara As String = Armory.GetUrlChara(Armory.GetBaseUrl(ChData.Region, AccessArmory.UrlCharactersInfo.UrlCharacterSheet), ChData.Realm, ChData.Character)

        html &= vbCrLf
        html &= "<h2 onclick=expand('charastatus')>Character Status</h2>" & vbCrLf
        html &= "<div id=charastatus>" & vbCrLf

        html &= "<p>Updated: " & ChData.LastModified & ", Crawled: " & Month(ChData.LastUpdate.Month - 1) & " " & Format(ChData.LastUpdate, "d, yyyy") & "</p>" & vbCrLf

        If ChData.ErrCode <> "" Then
            html &= "<p>Error <font color=red>" & ChData.ErrCode & "</font> occured.</p>"
        End If

        html &= "<p>" & vbCrLf
        html &= "<a href=""" & ArmoryUrlChara & """>" & ChData.Character & "</a>"
        If ChData.Guild = "" Then
            html &= ", "
        Else
            Dim ArmoryUrlGuild As String = Armory.GetUrlChara(Armory.GetBaseUrl(ChData.Region, AccessArmory.UrlCharactersInfo.UrlGuildInfo), ChData.Realm, ChData.Guild)
            html &= " of " & "<a href=""" & ArmoryUrlGuild & """>" & ChData.Guild & "</a>" & ", "
        End If
        html &= ChData.Realm & " in " & ChData.Region & "<br/>" & vbCrLf
        html &= ChData.Gender & " " & ChData.Race & " " & ChData.ClassName
        html &= " Level." & ChData.Level

        '--- talents
        If ChData.Talent <> "" Then
            Dim t As String = ""
            If ChData.Build1name = "" Then
                t = ""
            Else
                t = ChData.Build1name & " "
            End If

            Dim talpage As String
            talpage = "http://www.wowarmory.com/talent-calc.xml?c=" & LCase(Replace(ChData.ClassName, "+", "")) & "&tal=" & ChData.Talent
            html &= " (<A HREF=""" & talpage & """>" & t & ChData.Build1 & "/" & ChData.Build2 & "/" & ChData.Build3 & "</A>"

            If ChData.Talent2 IsNot Nothing Then
                If ChData.Talent2 <> "" Then
                    If ChData.Build2name = "" Then
                        t = ""
                    Else
                        t = ChData.Build2name & " "
                    End If
                    talpage = "http://www.wowarmory.com/talent-calc.xml?c=" & LCase(Replace(ChData.ClassName, "+", "")) & "&tal=" & ChData.Talent2
                    html &= ", <A HREF=""" & talpage & """>" & t & ChData.Build21 & "/" & ChData.Build22 & "/" & ChData.Build23 & "</A>"
                End If
            End If
            html &= ")" & "<br/>"
        End If

        '--- health
        If ChData.Health > 0 Then
            html &= "Health:" & ChData.Health
        End If
        If ChData.Mana > 0 Then
            html &= ", Mana:" & ChData.Mana
        End If
        html &= "</p>" & vbCrLf

        '--- profession
        If ChData.Prof1name <> "" Then
            html &= "<p>"
            html &= ChData.Prof1name & " " & ChData.Prof1value
            If ChData.Prof2name <> "" Then
                html &= ", "
                html &= ChData.Prof2name & " " & ChData.Prof2value
            End If
            html &= "</p>" & vbCrLf
        End If


        '--- glyph
        If ChData.Glyph IsNot Nothing Then
            Dim GlyphDB As New GlyphDB

            html &= "<table><tr valign=top>"

            html &= "<td><table border><th colspan=2>Glyph 1</th>"
            For i = 0 To ChData.Glyph.Length - 1
                Dim gdata As GlyphDB.Glyph = GlyphDB.GetGlyphInfo(ChData.Glyph(i))
                html &= "<tr align=center><th>" & GlyphCase(gdata.Type) & "</th><td>" & gdata.Name & "</td></tr>"
            Next
            html &= "</table></td>"

            If ChData.Glyph2 IsNot Nothing Then
                html &= "<td><table border><th colspan=2>Glyph 2</th>"
                For i = 0 To ChData.Glyph2.Length - 1
                    Dim gdata As GlyphDB.Glyph = GlyphDB.GetGlyphInfo(ChData.Glyph2(i))
                    html &= "<tr align=center><th>" & GlyphCase(gdata.Type) & "</th><td>" & gdata.Name & "</td></tr>"
                Next
                html &= "</table></td>"
            End If

            html &= "</tr></table>"
        End If


        '--- status
        html &= "<table border width=95%>" & vbCrLf
        html &= "<tr><th colspan=2 width=20%>Stats</th><th colspan=2 width=20%>Defense</th><th colspan=2 width=20%>Melee</th><th colspan=2 width=20%>Ranged</th><th colspan=2 width=20%>Spell</th></tr>" & vbCrLf

        html &= "<tr align=center> <th>Str</th> <td>" & ChData.Str & "</td> <th>Armor</th> <td>" & ChData.Armor & "</td> <th>Power</th><td>" & ChData.MeleePower & "</td> <th>Power</th><td>" & ChData.RangedPower & "</td> <th>SPwr</th><td>" & ChData.SpellPower & "</td></tr>" & vbCrLf
        html &= "<tr align=center> <th>Agi</th> <td>" & ChData.Agi & "</td> <th>Def</th><td>" & ChData.Defense & "</td> <th>Hit</th><td>" & ChData.MeleeHit & "</td> <th>Hit</th><td>" & ChData.RangedHit & "</td> <th>Hit</th><td>" & ChData.SpellHit & "</td></tr>" & vbCrLf
        html &= "<tr align=center> <th>Sta</th> <td>" & ChData.Sta & "</td> <th>Dodge</th><td>" & ChData.Dodge & "</td> <th>Crit</th><td>" & ChData.MeleeCrit & "</td> <th>Crit</th><td>" & ChData.RangedCrit & "</td> <th>Crit</th><td>" & ChData.SpellCrit & "</td></tr>" & vbCrLf
        html &= "<tr align=center> <th>Int</th> <td>" & ChData.Int & "</td> <th>Parry</th><td>" & ChData.Parry & "</td> <th>Haste</th><td>&nbsp;" & ChData.MeleeHasteMH & " / " & ChData.MeleeHasteOH & "</td> <th>Haste</th><td>&nbsp;" & ChData.RangedHaste & "</td> <th>Haste</th><td>" & ChData.SpellHaste & "</td></tr>" & vbCrLf
        html &= "<tr align=center> <th>Spi</th> <td>" & ChData.Spi & "</td> <th>Block</th><td>" & ChData.Block & "</td> <th>WDps</th><td>&nbsp;" & ChData.MeleeDpsMH & " / " & ChData.MeleeDpsOH & "</td> <th>WDps</th><td>&nbsp;" & ChData.RangedDps & "</td> <th>&nbsp;</th><td>" & "&nbsp;" & "</td></tr>" & vbCrLf
        html &= "<tr align=center> <th>&nbsp;</th> <td>" & "&nbsp;" & "</td> <th>Resil</th><td>" & ChData.Resilience & "</td> <th>WSpd</th><td>&nbsp;" & ChData.MeleeWSpeedMH & " / " & ChData.MeleeWSpeedOH & "</td> <th>WSpd</th><td>&nbsp;" & ChData.RangedWSpeed & "</td> <th>HPwr</th><td>" & ChData.HealPower & "</td></tr>" & vbCrLf
        html &= "<tr align=center> <th>&nbsp;</th> <td>" & "&nbsp;" & "</td> <th>&nbsp;</th><td>" & "&nbsp;" & "</td> <th>Penet</th><td>&nbsp;" & ChData.MeleePenet & "</td> <th>Penet</th><td>&nbsp;" & ChData.RangedPenet & "</td>"

        html &= "<th>MP5</th><td>"
        If ChData.ManaRegRest = 0 Then
            html &= ChData.ManaRegen
        Else
            html &= ChData.ManaRegen & " (" & ChData.ManaRegRest & ")"
        End If
        html &= "</td></tr>" & vbCrLf

        html &= "</table>" & vbCrLf
        html &= "<br/>" & vbCrLf
        html &= "</div>" & vbCrLf

        '--- gears

        Dim ItemDB = New WowCrawler.ItemDB
        'Dim EnchantDB = New WowCrawler.EnchantDB

        html &= vbCrLf
        html &= "<h2 onclick=expand('charagears')>Character Equipments</h2>" & vbCrLf
        html &= "<div id=charagears>" & vbCrLf

        Dim slotname() As String = {"Bullets", "Head", "Neck", "Shoulders", "Shirt", "Chest", "Belt", "Legs", "Feet", "Bracers", "Hands", "Ring1", "Ring2", "Trinket1", "Trinket2", "Back", "Main-Hand", "Off-Hand", "Ranged/Relic", "Tabard"}

        Dim table As String = ""
        If ChData.items IsNot Nothing Then
            table &= "<table border>" & vbCrLf
            'table &= "<tr><th>Slot</th><th>Level</th><th>Name</th><th>Enchant</th></tr>" & vbCrLf
            table &= "<tr><th>Slot</th><th>Level</th><th>Name</th></tr>" & vbCrLf

            For i = 0 To ChData.items.Length - 1
                table &= "<tr>"
                table &= "<td align=center>" & slotname(i) & "</td>"
                table &= "<td align=center>" & ItemDB.GetItemInfo(ChData.items(i)).Lvl & "</td>"
                table &= "<td><nobr>" & "<a href=""http://www.wowhead.com/?item=" & ChData.items(i) & """ class=""itemQlty" & ItemDB.GetItemInfo(ChData.items(i)).Qlty & """>" & ItemDB.GetItemInfo(ChData.items(i)).Name & "</a>" & "</nobr></td>"
                'If EnchantDB.GetEnchantInfo(chdata.ench(i)).Name = "Unknown" Then
                '    table &= "<td align=center>ID: " & EnchantDB.GetEnchantInfo(chdata.ench(i)).ID & "</td>"
                'Else
                '    table &= "<td align=center>" & EnchantDB.GetEnchantInfo(chdata.ench(i)).Name & "</td>"
                'End If
                table &= "</tr>" & vbCrLf
            Next

            table &= "</table>" & vbCrLf
        Else
            table &= "&nbsp;" & vbCrLf
        End If

        If ChData.iLvTotal > 0 Then
            html &= "<p>" & vbCrLf
            html &= "Items Level: total " & Format(ChData.iLvTotal, "###,###,##0") & " / average " & Format(ChData.iLvAve, "###,###,##0.0") & "<br/>" & vbCrLf
            html &= "<i>" & "(formula: Uncommon = (ilv - 8) x 0.6 + 26, Rare = (ilv - 0.75) x 0.667 + 26, Epic or higher = ilv)" & "</i><br/>" & vbCrLf
            html &= "</p>" & vbCrLf
        End If

        html &= "<table>"
        html &= "<tr><td valign=top>" & vbCrLf
        html &= table & vbCrLf
        html &= "</td>" & vbCrLf

        '--- gems
        html &= "<td valign=top>" & vbCrLf

        If ChData.gems IsNot Nothing Then
            Dim Gem(0) As Long
            Dim GemCount(0) As Long
            CountGems(ChData, Gem, GemCount)

            Dim g As Long

            html &= "<table border>" & vbCrLf
            html &= "<tr><th>Colour</th><th>Level</th><th>Name</th><th>Count</th></tr>" & vbCrLf
            For g = 0 To Gem.Length - 1
                html &= "<tr>"
                html &= "<td align=center>" & ItemDB.GetItemInfo(Gem(g)).Type & "</td>"
                html &= "<td align=center>" & ItemDB.GetItemInfo(Gem(g)).Lvl & "</td>"
                html &= "<td><nobr>" & "<a href=""http://www.wowhead.com/?item=" & Gem(g) & """ class=""itemQlty" & ItemDB.GetItemInfo(Gem(g)).qlty & """>" & ItemDB.GetItemInfo(Gem(g)).Name & "</a></nobr></td>"
                html &= "<td align=center>" & GemCount(g) & "</td>"
                html &= "</tr>" & vbCrLf
            Next
            html &= "</table>" & vbCrLf
            html &= "<br/>" & vbCrLf
        Else
            html &= "&nbsp;" & vbCrLf
        End If
        html &= "</td></tr>" & vbCrLf
        html &= "</table>" & vbCrLf
        html &= "</div>" & vbCrLf


        '--- Achievement

        If ChData.Achievements IsNot Nothing Then
            html &= vbCrLf
            html &= "<h2 onclick=expand('charaachieve')>Achievements</h2>" & vbCrLf
            html &= "<div id=charaachieve>" & vbCrLf

            html &= "<table border>" & vbCrLf
            html &= "<tr><th>Patch</th><th>Area</th><th>Boss</th><th>Note</th><th>10man</th><th>10man H</th><th>25man</th><th>25man H</th></tr>" & vbCrLf

            Dim AchDB As New WowCrawler.AchievementDB
            For i = 0 To AchDB.Achievements.Length - 1
                html &= "<tr align=center>"
                html &= "<td>&nbsp;" & AchDB.Achievements(i).ver & "&nbsp;</td>"
                html &= "<td>&nbsp;" & AchDB.Achievements(i).area & "&nbsp;</td>"
                html &= "<td>&nbsp;" & AchDB.Achievements(i).boss & "&nbsp;</td>"
                html &= "<td>&nbsp;" & AchDB.Achievements(i).note & "&nbsp;</td>"

                If AchDB.Achievements(i).d10 = "" Then
                    html &= "<td bgcolor=silver>&nbsp;</td>"
                Else
                    If AchDB.HasAchievement(AchDB.Achievements(i).d10, ChData.Achievements) Then
                        html &= "<td>X</td>"
                    Else
                        html &= "<td>-</td>"
                    End If
                End If

                If AchDB.Achievements(i).d10H = "" Then
                    html &= "<td bgcolor=silver>&nbsp;</td>"
                Else
                    If AchDB.HasAchievement(AchDB.Achievements(i).d10H, ChData.Achievements) Then
                        html &= "<td>X</td>"
                    Else
                        html &= "<td>-</td>"
                    End If
                End If

                If AchDB.Achievements(i).d25 = "" Then
                    html &= "<td bgcolor=silver>&nbsp;</td>"
                Else
                    If AchDB.HasAchievement(AchDB.Achievements(i).d25, ChData.Achievements) Then
                        html &= "<td>X</td>"
                    Else
                        html &= "<td>-</td>"
                    End If
                End If

                If AchDB.Achievements(i).d25H = "" Then
                    html &= "<td bgcolor=silver>&nbsp;</td>"
                Else
                    If AchDB.HasAchievement(AchDB.Achievements(i).d25H, ChData.Achievements) Then
                        html &= "<td>X</td>"
                    Else
                        html &= "<td>-</td>"
                    End If
                End If

                html &= "</tr>" & vbCrLf
            Next
            html &= "</table>" & vbCrLf
            html &= "</div>" & vbCrLf
        End If


        '--- 

        If Not OnlyBody Then
            If ChData.Guild = "" Then
                html = GetHtmlHeader(ChData.Character) & html & "</body></html>"
            Else
                html = GetHtmlHeader(ChData.Character & " / " & ChData.Guild) & html & "</body></html>"
            End If
        End If
        Return html
    End Function

    Public Function CharaInfoToStr(ByRef ChData As CharactersInfo.Characters) As String
        Dim Quality() As String = {"Poor", "Common", "Uncommon", "Rare", "Epic", "Legendary", "Heirloom", "Artifact", "(@_@!!!"}

        Dim html As String = ""
        Dim i As Integer

        If ChData.ClassName = "" Then Return ""

        html &= ChData.Character
        If ChData.Guild <> "" Then
            html &= " / " & ChData.Guild
        End If
        html &= vbCrLf
        html &= vbCrLf


        html &= "Updated: " & ChData.LastModified & ", Crawled: " & Month(ChData.LastUpdate.Month - 1) & " " & Format(ChData.LastUpdate, "d, yyyy") & vbCrLf
        If ChData.ErrCode <> "" Then
            html &= "Error " & ChData.ErrCode & " occured." & vbCrLf
        End If
        html &= vbCrLf

        html &= "- Character" & vbCrLf

        html &= ChData.Character
        If ChData.Guild = "" Then
            html &= ", "
        Else
            html &= " of " & ChData.Guild & ", "
        End If
        html &= ChData.Realm & " in " & ChData.Region & vbCrLf
        html &= ChData.Gender & " " & ChData.Race & " " & ChData.ClassName
        html &= " Lv." & ChData.Level

        '--- talents
        If ChData.Talent <> "" Then
            Dim t As String = ""
            If ChData.Build1name = "" Then
                t = ""
            Else
                t = ChData.Build1name & " "
            End If
            html &= " (" & t & ChData.Build1 & "/" & ChData.Build2 & "/" & ChData.Build3

            If ChData.Talent2 IsNot Nothing Then
                If ChData.Talent2 <> "" Then
                    If ChData.Build2name = "" Then
                        t = ""
                    Else
                        t = ChData.Build2name & " "
                    End If
                    html &= ", " & t & ChData.Build21 & "/" & ChData.Build22 & "/" & ChData.Build23
                End If
            End If
            html &= ")" & vbCrLf
        End If

        If ChData.Health > 0 Then
            html &= "Health:" & ChData.Health
        End If
        If ChData.Mana > 0 Then
            html &= ", Mana:" & ChData.Mana
        End If
        html &= vbCrLf

        '--- profession
        If ChData.Prof1name <> "" Then
            html &= ChData.Prof1name & " " & ChData.Prof1value
            If ChData.Prof2name <> "" Then
                html &= ", "
                html &= ChData.Prof2name & " " & ChData.Prof2value
            End If
            html &= vbCrLf
        End If
        html &= vbCrLf


        '--- glyph
        If ChData.Glyph IsNot Nothing Then
            html &= "- Glyphs" & vbCrLf

            Dim GlyphDB As New GlyphDB
            html &= "Glyph 1" & vbCrLf

            For i = 0 To ChData.Glyph.Length - 1
                Dim gdata As GlyphDB.Glyph = GlyphDB.GetGlyphInfo(ChData.Glyph(i))
                html &= GlyphCase(gdata.Type) & ": " & gdata.Name & vbCrLf
            Next
            html &= vbCrLf

            If ChData.Glyph2 IsNot Nothing Then
                html &= "Glyph 2" & vbCrLf
                For i = 0 To ChData.Glyph2.Length - 1
                    Dim gdata As GlyphDB.Glyph = GlyphDB.GetGlyphInfo(ChData.Glyph2(i))
                    html &= GlyphCase(gdata.Type) & ": " & gdata.Name & vbCrLf
                Next
                html &= vbCrLf
            End If
        End If

        '--- status
        html &= "- Status" & vbCrLf
        html &= "Status: "
        html &= "Str " & ChData.Str
        html &= ", Agi " & ChData.Agi
        html &= ", Sta " & ChData.Sta
        html &= ", Int " & ChData.Int
        html &= ", Spi " & ChData.Spi
        html &= vbCrLf

        html &= "Defense: "
        html &= "Armor " & ChData.Armor
        html &= ", Def " & ChData.Defense
        html &= ", Dodge " & ChData.Dodge
        html &= ", Parry " & ChData.Parry
        html &= ", Block " & ChData.Block
        html &= ", Resil " & ChData.Resilience
        html &= vbCrLf

        html &= "Melee: "
        html &= "APower " & ChData.MeleePower
        html &= ", Wdps " & ChData.MeleeDpsMH & "/" & ChData.MeleeDpsOH
        html &= ", Wspd " & ChData.MeleeWSpeedMH & "/" & ChData.MeleeWSpeedOH
        html &= ", Hit " & ChData.MeleeHit
        html &= ", Crit " & ChData.MeleeCrit
        html &= ", Penet " & ChData.MeleePenet
        html &= vbCrLf

        html &= "Ranged: "
        html &= "APower " & ChData.RangedPower
        html &= ", Wdps " & ChData.RangedDps
        html &= ", Wspd " & ChData.RangedWSpeed
        html &= ", Hit " & ChData.RangedHit
        html &= ", Crit " & ChData.RangedCrit
        html &= ", Penet " & ChData.RangedPenet
        html &= vbCrLf

        html &= "Spell: "
        html &= "SPower " & ChData.SpellPower
        html &= ", HPower " & ChData.HealPower
        html &= ", Hit " & ChData.SpellHit
        html &= ", Crit " & ChData.SpellCrit
        html &= ", Haste " & ChData.SpellHaste
        html &= ", MP5 "
        If ChData.ManaRegRest = 0 Then
            html &= ChData.ManaRegen
        Else
            html &= ChData.ManaRegen & " (" & ChData.ManaRegRest & ")"
        End If
        html &= vbCrLf

        html &= vbCrLf


        '--- gears

        Dim ItemDB = New WowCrawler.ItemDB

        html &= "- Gears" & vbCrLf

        Dim slotname() As String = {"Bullets", "Head", "Neck", "Shoulders", "Shirt", "Chest", "Belt", "Legs", "Feet", "Bracers", "Hands", "Ring1", "Ring2", "Trinket1", "Trinket2", "Back", "Main-Hand", "Off-Hand", "Ranged/Relic", "Tabard"}

        Dim table As String = ""
        If ChData.items IsNot Nothing Then
            For i = 0 To ChData.items.Length - 1
                table &= slotname(i) & ": "
                table &= ItemDB.GetItemInfo(ChData.items(i)).Name

                If ItemDB.GetItemInfo(ChData.items(i)).id <> 0 Then
                    table &= " (" & Quality(ItemDB.GetItemInfo(ChData.items(i)).Qlty)
                    table &= ", lv" & ItemDB.GetItemInfo(ChData.items(i)).Lvl & ")"
                End If
                table &= vbCrLf
            Next

            table &= vbCrLf
        Else
            table &= vbCrLf
        End If

        If ChData.iLvTotal > 0 Then
            html &= "Items Level: total " & Format(ChData.iLvTotal, "###,###,##0") & " / average " & Format(ChData.iLvAve, "###,###,##0.0") & vbCrLf
            html &= "(formula: Uncommon = (ilv - 8) x 0.6 + 26, Rare = (ilv - 0.75) x 0.667 + 26, Epic or higher = ilv)" & vbCrLf
            html &= vbCrLf
        End If
        html &= table

        '--- gems
        html &= "- Gems" & vbCrLf

        If ChData.gems IsNot Nothing Then
            Dim Gem(0) As Long
            Dim GemCount(0) As Long
            CountGems(ChData, Gem, GemCount)

            Dim g As Long

            For g = 0 To Gem.Length - 1
                html &= ItemDB.GetItemInfo(Gem(g)).Type & ": "
                html &= ItemDB.GetItemInfo(Gem(g)).Name
                html &= " (" & Quality(ItemDB.GetItemInfo(ChData.gems(g)).Qlty)
                html &= ", lv" & ItemDB.GetItemInfo(ChData.gems(g)).Lvl & ")"
                html &= " x " & GemCount(g) & " pcs" & vbCrLf
            Next
        Else
            html &= vbCrLf
        End If
        html &= vbCrLf


        '--- Achievement

        If ChData.Achievements IsNot Nothing Then
            html &= "- Achievements"

            Dim ver As String = ""
            Dim area As String = ""
            Dim AchDB As New WowCrawler.AchievementDB
            For i = 0 To AchDB.Achievements.Length - 1
                If area <> AchDB.Achievements(i).area Then
                    html &= vbCrLf
                    html &= AchDB.Achievements(i).area & ":" & vbCrLf
                End If
                area = AchDB.Achievements(i).area

                html &= AchDB.Achievements(i).boss
                If AchDB.Achievements(i).note <> "" Then
                    html &= " (" & AchDB.Achievements(i).note & ")"
                End If
                html &= ":"

                If AchDB.Achievements(i).d10 = "" Then
                    '
                Else
                    If AchDB.HasAchievement(AchDB.Achievements(i).d10, ChData.Achievements) Then
                        html &= " 10man"
                    Else
                        html &= " -"
                    End If
                End If

                If AchDB.Achievements(i).d10H = "" Then
                    '
                Else
                    If AchDB.HasAchievement(AchDB.Achievements(i).d10H, ChData.Achievements) Then
                        html &= " 10manH"
                    Else
                        html &= " -"
                    End If
                End If

                If AchDB.Achievements(i).d25 = "" Then
                    '
                Else
                    If AchDB.HasAchievement(AchDB.Achievements(i).d25, ChData.Achievements) Then
                        html &= " 25man"
                    Else
                        html &= " -"
                    End If
                End If

                If AchDB.Achievements(i).d25H = "" Then
                    '
                Else
                    If AchDB.HasAchievement(AchDB.Achievements(i).d25H, ChData.Achievements) Then
                        html &= " 25manH"
                    Else
                        html &= " -"
                    End If
                End If

                html &= vbCrLf
            Next
            html &= vbCrLf
        End If

        html &= "------------------------------------------------------------" & vbCrLf
        html &= "Created by Blue Forest WoW Armory Crawler, (c)2009 Odelle" & vbCrLf
        Return html
    End Function

    Private Function GlyphCase(ByVal GlyphType As String) As String
        Select Case LCase(GlyphType)
            Case "major"
                Return "MAJOR"
            Case "minor"
                Return "minor"
            Case Else
                Return GlyphType
        End Select
    End Function

    Public Sub CountGems(ByVal player As WowCrawler.CharactersInfo.Characters, ByRef Gem() As Long, ByRef GemCount() As Long)
        Dim i As Long
        Dim ct As Long = 0

        For i = 0 To player.gems.Length - 1
            If ct = 0 Then
                ReDim Gem(ct)
                ReDim GemCount(ct)

                Gem(ct) = player.gems(i)
                GemCount(ct) = 1
                ct += 1
            Else
                Dim j As Long
                Dim found As Boolean = False

                For j = 0 To ct - 1
                    If Gem(j) = player.gems(i) Then
                        GemCount(j) += 1
                        found = True
                        Exit For
                    End If
                Next

                If Not found Then
                    ReDim Preserve Gem(ct)
                    ReDim Preserve GemCount(ct)

                    Gem(ct) = player.gems(i)
                    GemCount(ct) = 1
                    ct += 1
                End If
            End If
        Next
    End Sub

    Public Function GetHtmlHeader(ByVal Title As String) As String
        Const MetaTaxt As String = "<meta http-equiv=""Content-type"" content=""text/html; charset=utf8"">" & vbCrLf

        Dim html As String = ""

        html = "<html><head>" & vbCrLf
        html &= MetaTaxt
        html &= GetJavaScript()
        html &= GetStyleSheet()

        If Title <> "" Then
            html &= "<title>" & Title & "</title>" & vbCrLf
        End If
        html &= "</head>" & vbCrLf
        html &= "<body>" & vbCrLf
        If Title <> "" Then
            html &= "<a name=top></a><h1>" & Title & "</h1>" & vbCrLf
        End If

        Return html
    End Function

    Public Function GetJavaScript() As String
        Dim html As String = ""

        '--- expand/collapse
        html &= "<script type ='text/javascript'>" & vbCrLf
        html &= "function expand( id ) {" & vbCrLf
        html &= "  if (document.all.item( id ).style.display == 'none') {" & vbCrLf
        html &= "    document.all.item( id ).style.display = 'inline';" & vbCrLf
        html &= "  } else {" & vbCrLf
        html &= "    document.all.item( id ).style.display = 'none';" & vbCrLf
        html &= "  }" & vbCrLf
        html &= "}" & vbCrLf
        html &= "</script>" & vbCrLf

        '--- wowhead popup
        html &= "<script type=""text/javascript"" src=""http://www.wowhead.com/widgets/power.js""></script>"

        html &= vbCrLf

        Return html
    End Function
    Public Function GetStyleSheet() As String
        Dim html As String = ""

        html &= "<style TYPE=""text/css"">" & vbCrLf
        html &= ".c_dk  {background-color: tomato;}" & vbCrLf
        html &= ".c_drd {background-color: navajowhite;}" & vbCrLf
        html &= ".c_hnt {background-color: lawngreen;}" & vbCrLf
        html &= ".c_mag {background-color: lightcyan;}" & vbCrLf
        html &= ".c_pal {background-color: lightpink;}" & vbCrLf
        html &= ".c_pri {background-color: white;}" & vbCrLf
        html &= ".c_rog {background-color: yellow;}" & vbCrLf
        html &= ".c_sha {background-color: lightskyblue; }" & vbCrLf
        html &= ".c_war {background-color: tan;}" & vbCrLf
        html &= ".c_wrl {background-color: plum;}" & vbCrLf
        html &= ".c_ukw {background-color: gray;}" & vbCrLf

        html &= ".itemQlty0 {color: gray;}" & vbCrLf
        html &= ".itemQlty1 {color: black;}" & vbCrLf
        html &= ".itemQlty2 {color: green;}" & vbCrLf
        html &= ".itemQlty3 {color: blue;}" & vbCrLf
        html &= ".itemQlty4 {color: purple;}" & vbCrLf
        html &= ".itemQlty5 {color: orange;}" & vbCrLf
        html &= ".itemQlty6 {color: red;}" & vbCrLf

        html &= "h1 { background-color: mistyrose; }" & vbCrLf
        html &= "h2 { background-color: lemonchiffon; }" & vbCrLf
        html &= "h3 { background-color: seashell; }" & vbCrLf
        html &= "th { background-color: silver; }" & vbCrLf
        html &= "</style>" & vbCrLf

        Return html
    End Function

    Public Function GuildRosterToHtml(ByVal Region As String, ByVal Realm As String, ByVal Guild As String, ByRef ChInfo As CharactersInfo, Optional ByVal OnlyBody As Boolean = True, Optional ByVal Filepath As String = "", Optional ByVal VersionFilter As Double = 0.0) As String
        If OnlyBody Then
            Filepath = ""
        End If

        Dim html As String = ""

        Dim armory As New AccessArmory
        'armory.Region = armory.getregiontype(Region)
        Dim GuildXml As New Xml.XmlDocument
        Dim GuildNode As Xml.XmlElement = Nothing
        Try
            GuildNode = armory.GetArmoryGuildInfo(Realm, Guild, GuildXml)
        Catch ex As Exception
        End Try
        If GuildNode Is Nothing Then
            Return ""
        End If

        Dim chara As CharactersInfo.Characters = Nothing

        '--- achievement header
        Dim AchDb As New AchievementDB
        Dim i As Integer

        'Dim VersionFilter As Single = 0.0
        'For i = 0 To AchDb.Achievements.Length - 1
        '    If VersionFilter <= AchDb.Achievements(i).ver Then
        '        VersionFilter = AchDb.Achievements(i).ver
        '    End If
        'Next
        'VersionFilter = 3.0

        Const DifficultyCount As Integer = 4
        Dim Diff(,) As Integer = Nothing
        Dim AchieveIdx() As Integer = Nothing
        Dim DiffName() As String = {"10", "10H", "25", "25H"}

        Dim AchieveIdxCount As Integer = 0

        For i = 0 To AchDb.Achievements.Length - 1

            If VersionFilter <= AchDb.Achievements(i).ver Then
                If i = 0 Then
                    ReDim Preserve AchieveIdx(AchieveIdxCount)
                    ReDim Preserve Diff(DifficultyCount - 1, AchieveIdxCount)
                    AchieveIdx(AchieveIdxCount) = i

                    Dim j As Integer
                    For j = 0 To DifficultyCount - 1
                        Diff(j, AchieveIdxCount) = 0
                    Next

                    AchieveIdxCount += 1
                Else
                    If AchDb.Achievements(AchieveIdx(AchieveIdxCount - 1)).area <> AchDb.Achievements(i).area Then
                        AchieveIdxCount += 1
                        ReDim Preserve AchieveIdx(AchieveIdxCount)
                        ReDim Preserve Diff(DifficultyCount - 1, AchieveIdxCount)
                        AchieveIdx(AchieveIdxCount) = i

                        Dim j As Integer
                        For j = 0 To DifficultyCount - 1
                            Diff(j, AchieveIdxCount) = 0
                        Next

                        AchieveIdxCount += 1
                    End If
                End If

                If AchDb.Achievements(i).d10 <> "" Then
                    Diff(0, AchieveIdxCount - 1) += 1
                End If
                If AchDb.Achievements(i).d10H <> "" Then
                    Diff(1, AchieveIdxCount - 1) += 1
                End If
                If AchDb.Achievements(i).d25 <> "" Then
                    Diff(2, AchieveIdxCount - 1) += 1
                End If
                If AchDb.Achievements(i).d25H <> "" Then
                    Diff(3, AchieveIdxCount - 1) += 1
                End If
            End If
        Next

        Dim AchieveCount As Integer = 0
        Dim h As String = ""
        For i = 0 To AchieveIdxCount - 1
            Dim j As Integer
            For j = 0 To DifficultyCount - 1
                If Diff(j, i) = 1 Then
                    h &= "<th>" & AchDb.Achievements(AchieveIdx(i)).area & " " & DiffName(j) & "</th>"
                    'h &= "<th>" & AchDb.Achievements(AchIdx(i)).area & " " & AchDb.Achievements(AchIdx(i)).boss & " " & DiffName(j) & "</th>"

                    AchieveCount += 1
                ElseIf Diff(j, i) > 1 Then
                    h &= "<th>" & AchDb.Achievements(AchieveIdx(i)).area & " " & DiffName(j) & " (" & Diff(j, i) & ")</th>"

                    AchieveCount += 1
                End If
            Next
        Next

        Dim header As String = ""
        header &= "<tr><th>Rank</th><th>Name</th><th>Level</th><th>Class</th><th>Race</th><th>Gender</th>"
        header &= "<th>Build1</th><th>Build2</th>"
        header &= "<th>iLevel</th><th>Health</th><th>Mana</th>"
        header &= h
        header &= "<th>Updated</th></tr>" & vbCrLf


        html &= "<table border>" & vbCrLf

        Dim ele As Xml.XmlElement
        Dim ct As Integer = 0
        For i = 0 To 9
            For Each ele In GuildNode.SelectNodes("character[@rank='" & i & "']")
                chara = ChInfo.GetCharactersInfo(ele.Attributes("name").Value, Realm, Region)
                If chara.ErrCode = "" Then

                    If ct Mod 20 = 0 Then
                        html &= header
                    End If

                    html &= "<tr align=center>"
                    html &= "<td>" & ele.Attributes("rank").Value & "</td>"
                    If Filepath <> "" Then
                        Dim chafilename As String = "character" & ct & ".html"
                        html &= "<td><a href=" & chafilename & ">" & chara.Character & "</a></td>"
                        Dim chahtml(0) As String
                        chahtml(0) = CharaInfoToHtml(chara, False)
                        chafilename = My.Computer.FileSystem.CombinePath(Filepath, chafilename)
                        Try
                            System.IO.File.WriteAllLines(chafilename, chahtml, System.Text.Encoding.UTF8)
                        Catch ex As Exception
                        End Try
                    Else
                        html &= "<td>" & chara.Character & "</td>"
                    End If

                    Dim shortClass As String = GetShortClassName(chara.ClassName)
                    html &= "<td>" & chara.Level & "</td>"
                    html &= "<td class=""c_" & shortClass & """>" & chara.ClassName & "</td>"
                    html &= "<td>" & chara.Race & "</td>"
                    html &= "<td>" & chara.Gender & "</td>"

                    '--- talents
                    Dim talent As String = ""

                    If chara.Build1name = "" Then
                        talent &= "<TD>&nbsp;</TD>"
                    Else
                        'talent &= "<TD>" & chara.Build1name & " (" & chara.Build1 & "/" & chara.Build2 & "/" & chara.Build3 & ")</TD>"
                        talent &= "<TD>" & chara.Build1name & "</TD>"
                    End If

                    If chara.Talent2 IsNot Nothing Then
                        If chara.Build2name = "" Then
                            talent &= "<TD>&nbsp;</TD>"
                        Else
                            'talent &= "<TD>" & chara.Build2name & " (" & chara.Build21 & "/" & chara.Build22 & "/" & chara.Build23 & ")</TD>"
                            talent &= "<TD>" & chara.Build2name & "</TD>"
                        End If
                    Else
                        talent &= "<TD>&nbsp;</TD>"
                    End If

                    html &= talent

                    '------

                    'html &= "<td>" & chara.Achieve & "</td>"
                    html &= "<td>" & chara.iLvAve & "</td>"
                    html &= "<td>" & chara.Health & "</td>"
                    html &= "<td>" & chara.Mana & "</td>"

                    Dim j As Integer
                    Dim DiffChara(DifficultyCount - 1, AchieveIdxCount - 1)
                    For j = 0 To AchieveIdxCount - 1
                        Dim Area As String = AchDb.Achievements(AchieveIdx(j)).area

                        Dim d As Integer

                        For d = 0 To DifficultyCount - 1
                            DiffChara(d, j) = 0
                        Next

                        If chara.Achievements IsNot Nothing Then
                            Dim a As Integer
                            For a = 0 To AchDb.Achievements.Length - 1
                                If AchDb.Achievements(a).area = Area And AchDb.Achievements(a).ver >= VersionFilter Then
                                    If AchDb.HasAchievement(AchDb.Achievements(a).d10, chara.Achievements) Then
                                        DiffChara(0, j) += 1
                                    End If
                                    If AchDb.HasAchievement(AchDb.Achievements(a).d10H, chara.Achievements) Then
                                        DiffChara(1, j) += 1
                                    End If
                                    If AchDb.HasAchievement(AchDb.Achievements(a).d25, chara.Achievements) Then
                                        DiffChara(2, j) += 1
                                    End If
                                    If AchDb.HasAchievement(AchDb.Achievements(a).d25H, chara.Achievements) Then
                                        DiffChara(3, j) += 1
                                    End If
                                End If
                            Next
                        End If

                        For d = 0 To DifficultyCount - 1
                            If Diff(d, j) > 0 Then
                                Dim colour As String = ""

                                If DiffChara(d, j) >= Diff(d, j) Then
                                    colour = "bgcolor=cyan"
                                ElseIf CDbl(DiffChara(d, j)) >= (CDbl(Diff(d, j)) / 2) Then
                                    colour = "bgcolor=yellow"
                                ElseIf DiffChara(d, j) > 0 Then
                                    colour = "bgcolor=pink"
                                End If

                                If colour = "" Then
                                    html &= "<td>&nbsp;</td>"
                                Else
                                    html &= "<td " & colour & ">" & DiffChara(d, j) & "</td>"
                                End If
                            End If
                        Next
                    Next

                    html &= "<td><nobr>" & chara.LastModified & "</nobr></td>"
                    html &= "</tr>"
                    html &= vbCrLf

                    'Else
                    '    html &= "<tr align=center>"
                    '    html &= "<td>" & ele.Attributes("rank").Value & "</td>"
                    '    html &= "<td>" & ele.Attributes("name").Value & "</td>"
                    '    html &= "<td>" & ele.Attributes("level").Value & "</td>"
                    '    html &= "<td>&nbsp;</td>"   ' class
                    '    html &= "<td>&nbsp;</td>"   ' race
                    '    html &= "<td>&nbsp;</td>"   ' gender

                    '    html &= "<td>&nbsp;</td>"   ' build1
                    '    html &= "<td>&nbsp;</td>"   ' build2

                    '    'html &= "<td>&nbsp;</td>"  ' achieve
                    '    html &= "<td>&nbsp;</td>"   ' ilvl
                    '    html &= "<td>&nbsp;</td>"   ' health
                    '    html &= "<td>&nbsp;</td>"   ' mana

                    '    Dim j As Integer
                    '    For j = 0 To AchieveCount - 1
                    '        html &= "<td>&nbsp;</td>"
                    '    Next

                    '    html &= "<td>&nbsp;</td>"
                    '    html &= "</tr>"
                    '    html &= vbCrLf
                End If


                ct += 1
            Next
        Next

        html &= "</table>" & vbCrLf

        If Not OnlyBody Then
            html = GetHtmlHeader("Guild Roster of " & Guild & " / " & Realm & " / " & Region) & html & "</body></html>"
        End If

        If Filepath <> "" Then
            Dim filename As String = "index.html"
            Dim idxhtml(0) As String
            idxhtml(0) = html
            filename = My.Computer.FileSystem.CombinePath(Filepath, filename)
            Try
                System.IO.File.WriteAllLines(filename, idxhtml, System.Text.Encoding.UTF8)
            Catch ex As Exception
            End Try
        End If

        Return html
    End Function

    Public Function GetShortClassName(ByVal LongClassName As String) As String
        Dim Classes() As String = {"dk", "drd", "hnt", "mag", "pal", "pri", "rog", "sha", "war", "wrl", "ukw"}
        Dim Classes2() As String = {"Death Knight", "Druid", "Hunter", "Mage", "Paladin", "Priest", "Rogue", "Shaman", "Warrior", "Warlock", "Unknown"}

        Dim i As Integer

        For i = 0 To Classes2.Length - 1
            If LongClassName = Classes2(i) Then
                Return Classes(i)
            End If
        Next

        Return "ukw"
    End Function
End Class
